package com.lemon.boot.common.filter;

import com.lemon.boot.common.handler.HttpServletRequestHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 李猛
 * @datetime 2024/3/6 16:40
 * @description 请求体参数重复读取过滤器
 */
@Slf4j
public class RequestBodyRepeatReadFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        boolean isJsonRequest = request instanceof HttpServletRequest && MediaType.APPLICATION_JSON_VALUE.equalsIgnoreCase(request.getContentType());
        log.info("RequestBodyRepeatReadFilter.doFilter.isJsonRequest:{}", isJsonRequest);
        if (isJsonRequest) {
            chain.doFilter(new HttpServletRequestHandler((HttpServletRequest) request), response);
        } else {
            chain.doFilter(request, response);
        }
    }
}
